﻿log("Running Taobao Searcher 2  ...  searchTaobao2.js")

// current position
isIndexPage = function ()
{
    return location.href.search("www.taobao.com") != -1;
}
isSearchPage = function ()
{
    return location.href.search("s.taobao.com/search?") != -1;
}

// index
getSearchEdit = function ()
{
    return q;
}
getSubmitButton = function ()
{
    return document.querySelector(".btn-search");
}

// search methods
priceSetted = function ()
{
    return location.href.search(/&filter=.*_price/) != -1;
}
getSortMethod = function ()
{
    var match = /&sort=([^&]*)/.exec(location.href);
    if (!match)
        return "defaultSort";
    switch (match[1])
    {
        case "default":
            return "defaultSort";
        case "renqi-desc":
            return "populartySort";
        case "sale-desc":
            return "salesSort";
        case "credit-desc":
            return "creditSort";
        case "price-asc":
            return "priceSortAsc";
        case "price-desc":
            return "priceSort";
        case "total-asc":
            return "totalCostSortAsc";
        case "total-desc":
            return "totalCostSort";
        default:
            return "defaultSort";
    }
}
locationSetted = function ()
{
    return location.href.search("&loc=") != -1;
}

// price 
getItemLowerPriceEdit = function ()
{
    return document.querySelector("#J_relative > div.sort-row > div > div.prices > div.inputs.J_LaterHover > div > ul > li:nth-child(1) > input");
}
getItemHigherPriceEdit = function ()
{
    return document.querySelector("#J_relative > div.sort-row > div > div.prices > div.inputs.J_LaterHover > div > ul > li:nth-child(3) > input");
}
getSubmitPriceButton = function ()
{
    return document.querySelector("#J_relative > div.sort-row > div > div.prices > div.inputs.J_LaterHover > div > ul > li.submit > button");
}

// sort by
getSortByDefaultButton = function ()
{
    var as = document.querySelectorAll("#J_relative > div.sort-row > div > ul a");
    for (var i in as)
    {
        var a = as[i];
        if (a.text.search("综合") != -1)
            return a;
    }
}
getSortBySalesButton = function ()
{
    var as = document.querySelectorAll("#J_relative > div.sort-row > div > ul a");
    for (var i in as)
    {
        var a = as[i];
        if (a.text.search("销量") != -1)
            return a;
    }
}
getSortByPolulartyButton = function ()
{
    var as = document.querySelectorAll("#J_relative > div.sort-row > div > ul a");
    for (var i in as)
    {
        var a = as[i];
        if (a.text.search("人气") != -1)
            return a;
    }
}

// location
getLocationButton = function (loc)
{
    var top = document.querySelector("#J_relative > div.sort-row > div > div.location.J_LaterHover");
    var a = top.querySelectorAll("a");
    for (var i = 0; i < a.length; i++)
    {
        if (a[i].text.search(loc) != -1)
            return a[i];
    }
    return null;
}

// items 
getTops = function getTops()
{
    return document.querySelectorAll("div.item");
}
getA = function getA(_top)
{
    return _top.querySelector(".title a");
}
parseTop = function parseTop(_top)
{
    var img = _top.querySelector(".img");
    var a = getA(_top);
    var id = a ? a.getAttribute("data-nid") : null;
    var price = _top.querySelector(".price");
    var shop = _top.querySelector(".shopname");
    var _location = _top.querySelector(".location");

     return {
        extra: {
            top: _top
        },
        title: a ? fixString(a.textContent) : "",
        id: id ? id : "",
        price: price ? fixPrice(fixString(price.textContent)) : "",
        shop: shop ? fixString(shop.textContent) : "",
        link: a ? a.href : "",
        imgsrc: https2http(img ? ((img.src && img.src) != "" ? img.src : ("http:" + img.getAttribute("data-src"))) : ""),
        location: _location ? fixString(_location.textContent) : ""
    };
}

nextPageButton = function ()
{
    return document.querySelector("#mainsrp-pager > div > div > div > ul > li.item.next > a");
}